package com.mission.schedule.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.activity.QDFXDetailActivity;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.ScreenUtil;
import com.mission.schedule.utils.Utils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liwei on 2017/2/7.
 */

public class QDLongImageSaveAdapter3 extends RecyclerView.Adapter {

    private Context context;
    Handler handler;

    public List<NoteTitleDetailBean.ListBean> list = new ArrayList<>();

    public boolean header_image_flag = false;//false没有图片 true有图

    int maxTitleId = 0;

    int userid = 0;

    int appicon = 1;
    String copystyle = "0";
    String name = "";


    public List<NoteTitleDetailBean.ListBean> getData() {
        return list;
    }


    /**
     * @param context
     * @param list
     * @param maxTitleId 标题id
     * @param userid     用户id
     * @param appicon    清单图标
     * @param copystyle  粘贴位置
     * @param style      清单展示样式
     */
    public QDLongImageSaveAdapter3(Context context, List<NoteTitleDetailBean.ListBean> list, int maxTitleId, int userid
            , int appicon, String copystyle, int style, String Title , String name) {
        this.context = context;
        this.list = list;
        this.maxTitleId = maxTitleId;
        this.userid = userid;
        this.appicon = appicon;
        this.copystyle = copystyle;
        this.style = style;
        this.Title = Title;
        this.name = name;
    }


    String Title = "";

    public String getTitle() {
        return Title;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = new ViewTwo(LayoutInflater.from(context).inflate(R.layout.longimage_qdlist_item, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        }
        super.onBindViewHolder(holder, position, payloads);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final ViewTwo vo = (ViewTwo) holder;

        if (style == 0) {
            vo.ll.setVisibility(View.VISIBLE);
            vo.ch.setVisibility(View.VISIBLE);
            if (list.get(position).endstate == 0) {
                vo.ch.setBackground(context.getResources().getDrawable(R.mipmap.btn_qd_unfinish_c));
            } else {
                vo.ch.setBackground(context.getResources().getDrawable(R.mipmap.btn_qd_finish_c));
            }
            vo.dd.setVisibility(View.GONE);
            vo.xh.setVisibility(View.GONE);
            vo.santai.setVisibility(View.GONE);
        } else if (style == 1) {
            vo.ll.setVisibility(View.VISIBLE);
            vo.ch.setVisibility(View.GONE);
            vo.dd.setVisibility(View.VISIBLE);
            vo.xh.setVisibility(View.GONE);
            vo.santai.setVisibility(View.GONE);
        } else if (style == 2) {
            vo.ll.setVisibility(View.VISIBLE);
            vo.ch.setVisibility(View.GONE);
            vo.dd.setVisibility(View.GONE);
            vo.xh.setVisibility(View.VISIBLE);
            vo.santai.setVisibility(View.GONE);
        } else if (style == 3) {
            vo.ch.setVisibility(View.GONE);
            vo.dd.setVisibility(View.GONE);
            vo.xh.setVisibility(View.GONE);
            vo.ll.setVisibility(View.GONE);
            vo.santai.setVisibility(View.GONE);
            vo.emty_v.setVisibility(View.VISIBLE);
        } else if (style ==4) {
            vo.ch.setVisibility(View.GONE);
            vo.dd.setVisibility(View.GONE);
            vo.xh.setVisibility(View.GONE);
            vo.ll.setVisibility(View.VISIBLE);
            vo.santai.setVisibility(View.VISIBLE);
            if (list.get(position).endstate == 0) {
                vo.santai.setBackground(context.getResources().getDrawable(R.mipmap.icon_santai1));
            } else if (list.get(position).endstate == 1){
                vo.santai.setBackground(context.getResources().getDrawable(R.mipmap.icon_santai2));
            }else {
                vo.santai.setBackground(context.getResources().getDrawable(R.mipmap.icon_santai3));
            }
        }

        //-------------------------------------
        if (position == 0) {
            vo.title_ll.setVisibility(View.VISIBLE);
            String time = DateUtil.nowTime().substring(5, DateUtil.nowTime().length() - 3);
            if (list.get(position).createTime != null) {
                time = list.get(position).createTime.substring(5, list.get(position).createTime.length() - 3).replace('T', ' ');
            }
            vo.username.setText("@" + name + " " + time);
        } else {
            vo.title_ll.setVisibility(View.GONE);
        }


        if (list.get(position).contents.isEmpty()){
            vo.child_l.setVisibility(View.GONE);
        }

        if (Title.isEmpty()) {
            if (list.get(0).contents != null) {
                if (list.get(0).contents.isEmpty()) {
                    vo.title_tv.setVisibility(View.GONE);
                } else {
                    if (list.get(0).contents.length() > 15) {
                        vo.title_tv.setText(list.get(0).contents.substring(0, 15));
                    } else {
                        vo.title_tv.setText(list.get(0).contents);
                    }
                }
            } else {
                vo.title_tv.setVisibility(View.GONE);
            }
        } else {
            vo.title_tv.setText(Title);
        }

        vo.title_tv.setVisibility(View.VISIBLE);

        //需要添加一个判断本地是否有图片,有网络图片的时候
        if (position == 0) {
            if (list.get(position).imgPath != null) {
                if (!list.get(position).imgPath.equals("")) {
                    header_image_flag = true;

                    DisplayImageOptions options = new DisplayImageOptions.Builder()
                            .showStubImage(R.mipmap.loads)          // 设置图片下载期间显示的图片
                            .showImageForEmptyUri(R.mipmap.loads)  // 设置图片Uri为空或是错误的时候显示的图片
                            .showImageOnFail(R.mipmap.loads)       // 设置图片加载或解码过程中发生错误显示的图片
                            .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                            .cacheOnDisc(true)                          // 设置下载的图片是否缓存在SD卡中
                            .build();
                    String imageUrl = URLConstants.图片 + list.get(position).imgPath + "&imageType=15&imageSizeType=1";
                    ImageLoader.getInstance().displayImage(imageUrl, vo.header_image, options);

                    vo.header_image.setVisibility(View.VISIBLE);
                    vo.image_line.setVisibility(View.GONE);
                } else {
                    header_image_flag = false;
                    vo.header_image.setVisibility(View.GONE);
                    vo.image_line.setVisibility(View.VISIBLE);
                }
            } else {
                header_image_flag = false;
                vo.header_image.setVisibility(View.GONE);
                vo.image_line.setVisibility(View.VISIBLE);
            }
        }

        vo.content_tv.setVisibility(View.VISIBLE);
        vo.content_tv.setVisibility(View.VISIBLE);


//        vo.content_tv.setText(list.get(position).contents);
//        vo.editText.setText(list.get(position).contents);
        vo.xh.setText((position + 1) + ".");

        setcolor(list.get(position).contents, vo.content_tv);

        vo.longimage_foolter.setVisibility(View.GONE);


//        if (list.get(position).endstate == 0) {
//            vo.ch.setBackgroundResource(R.mipmap.btn_qd_unfinish_c);
//        } else {
//            vo.ch.setBackgroundResource(R.mipmap.btn_qd_finish_c);
//        }

        if (position == list.size() - 1) {
            vo.longimage_foolter.setVisibility(View.VISIBLE);
            if (position < 5) {
                if (list.get(position).imgPath != null) {
                    if (list.get(position).imgPath.equals("")) {
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.dipTopx(context, 380));
                        vo.longimage_foolter.setLayoutParams(params);
                    }
                }
            }
        } else {
            vo.longimage_foolter.setVisibility(View.GONE);
        }

    }


    //设置双#号包裹的文字颜色
    private void setcolor(String content, TextView editText) {
        if (content.length() > 1) {
            SpannableStringBuilder builder = new SpannableStringBuilder(content);
            List<Integer> integerList = new ArrayList<>();
            int index = 0;
            int count = hit(content, "#");
            if (count > 1) {
                if (count % 2 == 0) {
                    for (int i = 0; i < count; i++) {
                        if (i == 0) {
                            index = content.indexOf("#");
                        } else {
                            index = content.indexOf("#", index + 1);
                        }
                        integerList.add(index);
                    }
                    for (int i = 0; i < integerList.size(); i++) {
                        if (i > 0) {
                            if (i % 2 != 0) {
                                ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#df7926"));
                                builder.setSpan(span, integerList.get(i - 1), integerList.get(i) + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                            }
                        }
                    }
                    editText.setText(builder);
                } else {
                    for (int i = 0; i < count - 1; i++) {
                        if (i == 0) {
                            index = content.indexOf("#");
                        } else {
                            index = content.indexOf("#", index + 1);
                        }
                        integerList.add(index);

                    }
                    for (int i = 0; i < integerList.size(); i++) {
                        if (i > 0) {
                            if (i % 2 != 0) {
                                ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#df7926"));
                                builder.setSpan(span, integerList.get(i - 1), integerList.get(i) + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                            }
                        }
                    }
                    editText.setText(builder);
                }
            } else {
                editText.setText(content);
            }
        } else {
            editText.setText(content);
        }
        editText.setLineSpacing(20,1.5f);
    }

    /**
     * @param a 被匹配的长字符串
     * @param b 匹配的短字符串
     * @return 匹配次数
     */
    public int hit(String a, String b) {
        if (a.length() < b.length()) {
            return 0;
        }
        char[] a_t = a.toCharArray();
        int count = 0;

        for (int i = 0; i < a.length(); i++) {
            StringBuffer buffer = new StringBuffer();
            for (int j = 0; j < b.length(); j++) {
                buffer.append(a_t[i + j]);
            }
            if (buffer.toString().equals(b)) {
                count++;
            }
        }

        return count;
    }

    public int style = 0;


    @Override
    public int getItemCount() {
        return list.size();
    }


    public class ViewTwo extends RecyclerView.ViewHolder {

        public TextView xh, content_tv, title_tv, username;
        public ImageView ch;
        public ImageView dd, header_image,santai;
        public LinearLayout longimage_foolter, child_ll, child_l, title_ll;
        private RelativeLayout title_rl, ll;
        private View image_line, emty_v;

        public ViewTwo(View v) {
            super(v);
            image_line = v.findViewById(R.id.image_line);
            emty_v = v.findViewById(R.id.emty_v);
            username = (TextView) v.findViewById(R.id.username);
            xh = (TextView) v.findViewById(R.id.xh);
            content_tv = (TextView) v.findViewById(R.id.content_tv);
            ch = (ImageView) v.findViewById(R.id.ch);
            dd = (ImageView) v.findViewById(R.id.dd);
            santai = (ImageView) v.findViewById(R.id.santai);
            longimage_foolter = (LinearLayout) v.findViewById(R.id.longimage_foolter);
            child_ll = (LinearLayout) v.findViewById(R.id.child_ll);
            child_l = (LinearLayout) v.findViewById(R.id.child_l);

            header_image = (ImageView) v.findViewById(R.id.header_image);
            title_tv = (TextView) v.findViewById(R.id.title_tv);
            title_rl = (RelativeLayout) v.findViewById(R.id.title_rl);
            ll = (RelativeLayout) v.findViewById(R.id.ll);
            title_ll = (LinearLayout) v.findViewById(R.id.title_ll);
        }
    }
}
